package hu.bme.mit.inf.seample.gen;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;

import seample.GeneratorModel;
import seample.SeamSystem;

/**
 * Descriptor of a generation for Seam platform. Project names, package names, locations.
 * 
 * @author Mark Czotter
 * 
 */
public class SeamGenerationDescriptor {
	
	private SeamSystem seamSystem;
	private IProject project;
	private String ejbProjectName;
	private String entityPackge;
	private String sessionPackage;
	private String restPackage;
	private String wsPackage;
	private List<String> allPackageNames;
	private GeneratorModel genModel;
	private String earProjectName;

	/**
	 * Creates a default descriptor for generation based on the given generatorModel.
	 * @param genModel
	 */
	public SeamGenerationDescriptor(GeneratorModel genModel) {
		ejbProjectName = genModel.getProjectName();
		earProjectName = ejbProjectName;
		this.entityPackge = genModel.getDomainPackageName();
		this.sessionPackage = genModel.getBusinessPackageName();
		// genModel parent is a Collection of genModels, this collection's parent
		// is a SeamSystem
		this.genModel = genModel;
		this.seamSystem = (SeamSystem) genModel.eContainer().eContainer();
		Assert.isNotNull(seamSystem);
		initSubPackages();
	}
	
	/**
	 * Creates a descriptor for the given genModel, but some extra parameters is given for the Seam Web Project generation.
	 * @param seamWebProjectName
	 * @param entityPackge
	 * @param sessionPackage
	 */
	public SeamGenerationDescriptor(GeneratorModel genModel, IProject ejbProject, String earProjectName, String entityPackge, String sessionPackage) {
		this(genModel);
		this.ejbProjectName = ejbProject.getName();
		this.earProjectName = earProjectName;
		this.project = ejbProject;
		this.entityPackge = entityPackge;
		this.sessionPackage = sessionPackage;
		initSubPackages();
	}
	
	private void initSubPackages() {
		this.restPackage = getSessionPackage() + ".rest";
		this.wsPackage = getSessionPackage() + ".ws";
	}
	
	public String getEarProjectName() {
		return earProjectName;
	}

	public SeamSystem getSeamSystem() {
		return seamSystem;
	}

	/**
	 * Returns the EJB project or the simple project.
	 * @return
	 */
	public IProject getProject() {
		return project;
	}

	public String getLocationRoot() {
		return getProject().getLocation().toOSString() + "/";
	}

	public void setProject(IProject project) {
		this.project = project;
	}
	
	/**
	 * Returns the entity package (domain package).
	 * @return
	 */
	public String getEntityPackge() {
		return entityPackge;
	}
	
	/**
	 * Returns the session package (business package).
	 * @return
	 */
	public String getSessionPackage() {
		return sessionPackage;
	}
	
	public String getRestPackage() {
		return restPackage;
	}
	
	public String getWsPackage() {
		return wsPackage;
	}

	public String getEjbProjectName() {
		return ejbProjectName;
	}

	public List<String> getAllPackageNames() {
		if (allPackageNames == null) {
			allPackageNames = new ArrayList<String>();
			allPackageNames.add(getEntityPackge());
			allPackageNames.add(getSessionPackage());
			allPackageNames.add(getRestPackage());
			allPackageNames.add(getWsPackage());
		}
		return allPackageNames;
	}
	
	public GeneratorModel getGenModel() {
		return genModel;
	}

}
